//
//  EmptyShow.m
//  KidReading
//
//  Created by linziyuan on 16/4/8.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "EmptyShow.h"
#import "ConstDefine.h"

@interface EmptyShow ()

@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIButton *btn;

@end

@implementation EmptyShow


- (void)emptyShowWithType:(EmptyShowType)showtype tapEnabel:(BOOL)tapenable hideView:(UIView *)view defineStr:(NSString *)defineStr{
    [self.topActivety stopAnimating];
    if (view) {
        view.hidden = YES;
    }
    //
    self.textLabel.text = @"";
    
    self.iconView.image = nil;
    self.btn.enabled = NO;
    //
     NSDictionary *elementDic = [self getElementsWithType:showtype];
    //
    CGFloat fontSize = 30.0;
    CGFloat iconTop = 250.0;
    CGSize iconSize = CGSizeMake(168, 147);
    CGFloat labelHeight = 35.0;

    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        fontSize = 20.0;
        iconTop = 150.0;
        iconSize = CGSizeMake(168 * 0.7, 147 * 0.7);
        labelHeight = 60.0;
    }
    
    if (!self.iconView) {
        self.iconView = [[UIImageView alloc] init];
        self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:self.iconView];
        [self.iconView lyt_setSize:iconSize];
        [self.iconView lyt_centerXInParent];
        [self.iconView lyt_alignTopToView:self margin:iconTop];
    }
    
    UIImage *img = [UIImage imageNamed:[elementDic valueForKey:@"image"]];
    self.iconView.image = img;
    self.iconView.alpha = 0.8;
    
    //
    if (!self.textLabel) {
        self.textLabel = [[UILabel alloc] init];
        self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
        //self.textLabel.backgroundColor = [UIColor blueColor];
        [self addSubview:self.textLabel];
        //
        [self.textLabel lyt_alignLeftToParentWithMargin:30];
        [self.textLabel lyt_alignRightToParentWithMargin:30];
        [self.textLabel lyt_alignTopToView:self.iconView margin:(15+iconSize.height)];
        //[self.textLabel lyt_setHeight:labelHeight];
        self.textLabel.alpha = 0.3;
    }
    
    
    NSString *str = [elementDic valueForKey:@"title"];
    if (defineStr && ![defineStr isEqualToString:@""]) {
        str = defineStr;
    }
    [self.textLabel setText:str];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.numberOfLines = 0;
    self.textLabel.font = [UIFont systemFontOfSize:fontSize];
    self.textLabel.textColor = [UIColor colorWithHexString:@"6F6F6F"];//[[UIColor blackColor] colorWithAlphaComponent:0.3];
    
    if (!self.btn) {
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.frame = self.bounds;
        [self addSubview:self.btn];
        [self.btn addTarget:self action:@selector(tapReload) forControlEvents:UIControlEventTouchUpInside];
        self.btn.enabled = NO;
    }
    if (tapenable) {
        self.btn.enabled = YES;
    }
}

- (void)tapReload{
    [self.emptyDelegate tapEmptyShow];
}

- (void)dismissEmptyShowView:(UIView *)view {
    if (!self) {
        return;
    }else {
        self.textLabel.text = @"";
        [self.topActivety stopAnimating];
        self.iconView.image = nil;
        //[self removeAllSubViews];
    }
    if (view) {
        view.hidden = NO;
    }
}

- (void)modifyTheIconViewSize:(CGSize)iconSize topSpace:(CGFloat)topSpace labelHeight:(CGFloat)labelHeight {
    if (self.iconView) {
        if (iconSize.width > 0 && iconSize.height > 0) {
            if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
                iconSize = CGSizeMake(iconSize.width * 0.7, iconSize.height * 0.7);
            }
            [self.iconView lyt_setSize:iconSize];
        }
        if (topSpace > 0) {
            if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
                topSpace = topSpace * 0.6;
            }
          [self.iconView lyt_alignTopToView:self margin:topSpace];
        }
    }
    if (self.textLabel && labelHeight > 0) {
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            labelHeight = labelHeight - 3;
        }
        [self.textLabel lyt_setHeight:labelHeight];
    }
}

- (NSDictionary *)getElementsWithType:(EmptyShowType)type {
    
    NSDictionary *dic = nil;
    switch (type) {
        case EmptyShowType_Defualt:
            dic = @{@"title":NSLocalizedString(@"needtoRetry", ""),
                    @"image":@"emicon_wawayaya.png"};
            break;
        case EmptyShowType_MyBook:
            dic = @{@"title":NSLocalizedString(@"nomybooks", ""),
                    @"image":@"emicon_books.png"};
            break;
        case EmptyShowType_Fav:
            dic = @{@"title":NSLocalizedString(@"nofavorite", ""),
                    @"image":@"emicon_favorites.png"};
            break;
        case EmptyShowType_ParentRm:
            dic = @{@"title":NSLocalizedString(@"norecommendpar", ""),
                    @"image":@"emicon_parents.png"};
            break;
        case EmptyShowType_TeacherRm:
            dic = @{@"title":NSLocalizedString(@"norecommendtea", ""),
                    @"image":@"emicon_teacher.png"};
            break;
        case EmptyShowType_Paid:
            dic = @{@"title":NSLocalizedString(@"nopaid", ""),
                    @"image":@"emicon_purchase.png"};
            break;
        case EmptyShowType_Loading:
            dic = @{@"title":@"",
                    @"image":@"emicon_wawayaya.png"};
            break;
        case EmptyShowType_Web:
            dic = @{@"title":NSLocalizedString(@"needtoRetry", ""),
                    @"image":@"emicon_wawayaya.png"};
            break;
        default:
            break;
    }
    return dic;
}

@end
